package com.oschat.controller;

import com.oschat.annotation.GlobalInterceptor;
import com.oschat.entity.config.AppConfig;
import com.oschat.entity.constants.Constants;
import com.oschat.entity.enums.AppUpdateFileTypeEnum;
import com.oschat.entity.po.AppUpdate;
import com.oschat.entity.query.AppUpdateQuery;
import com.oschat.entity.vo.AppUpdateVO;
import com.oschat.entity.vo.PaginationResultVO;
import com.oschat.entity.vo.ResponseVO;
import com.oschat.service.AppUpdateService;
import com.oschat.utils.CopyTools;
import com.oschat.utils.StringTools;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author 黄宏展
 * @version 1.0
 * @description: 检测
 * @date 2025/8/26 16:04
 */
@RestController("updateController")
@RequestMapping("/update")
public class UpdateController extends ABaseController {

    @Resource
    private AppUpdateService appUpdateService;

    @Resource
    private AppConfig appConfig;

    @RequestMapping("/checkVersion")
    @GlobalInterceptor
    public ResponseVO checkVersion(String appVersion,String uid){
        if (StringTools.isEmpty(appVersion)){
            return getSuccessResponseVO(null);
        }
        AppUpdate appUpdate = appUpdateService.getLatestUpdate(appVersion, uid);
        if (appUpdate == null){
            return getSuccessResponseVO(null);
        }
        AppUpdateVO updateVO = CopyTools.copy(appUpdate,AppUpdateVO.class);
        if (AppUpdateFileTypeEnum.LOCAL.getType().equals(appUpdate.getFileType())){
            File file = new File(appConfig.getProjectFolder()+ Constants.APP_UPDATE_FOLDER+appUpdate.getId()+Constants.APP_EXE_SUFFIX);
            updateVO.setSize(file.length());
        }else {
            updateVO.setSize(0L);
        }
        updateVO.setUpdateList(Arrays.asList(appUpdate.getUpdateDescArray()));
        String fileName =Constants.APP_NAME+appUpdate.getVersion()+Constants.APP_EXE_SUFFIX;
        updateVO.setFileName(fileName);
        return getSuccessResponseVO(updateVO);
    }

}
